package edu.pet.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.PetType;
import edu.pet.entity.Type;

/**
 * Servlet implementation class PetTypeServlet
 */
public class PetTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PetTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		PetDAO dao = new PetDAO();
		Type petType = new Type();
		List<Type> typeList = new ArrayList<Type>();
		if("add".equals(type)){
			petType.setType(request.getParameter("name"));
			dao.addPetType(petType);
			this.getServletContext().getRequestDispatcher("/servlet/PetTypeServlet?type=list").forward(request, response);
		}
		if("edit".equals(type)){
			petType.setType(request.getParameter("name"));
			petType.setType_id(Integer.parseInt(request.getParameter("id")));
			dao.editPetType(petType);
			this.getServletContext().getRequestDispatcher("/servlet/PetTypeServlet?type=list").forward(request, response);
		}
		if("del".equals(type)){
			if(dao.delType(Integer.parseInt(request.getParameter("id")))){
				request.setAttribute("msg", "删除成功");
			}else{
				request.setAttribute("msg", "该类别正在使用中！");
			}
			this.getServletContext().getRequestDispatcher("/servlet/PetTypeServlet?type=list").forward(request, response);
		}
		if("list".equals(type)){
			//默认处理
			typeList = dao.getType();
			request.setAttribute("typeList", typeList);
			this.getServletContext().getRequestDispatcher("/user/manage/petTypeList.jsp").forward(request, response);
		}
		
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
